You are wondering what is a viewData
in MVC? In this tutorial, you will learn about ViewData
objects and how we use them in ASP.NET Applications.
What is ViewData in ASP.NET MVC?
ViewData is an instance of the ViewDataDictionary
class and is used to transfer data from the controller to view. It can be accessed by using a string as a key while the data is stored in ViewData
as an object.
Before using the data, you need to cast the data to its original type. While retrieving the data you can also verify the data against NULL value.
The ViewData
is only accessible for the current request when the request is ended.
Example of ViewData in MVC
In the following example, we will create a ViewData instance in the controller and display the data in View.
Controller
public class HomeController : Controller { public ActionResult Index() { ViewData["WelcomeMessage"] = "Welcome to TutorialsPanel.com!"; return View(); } }
View
@{ ViewBag.Title = "Home Page"; } <br /> <div> @ViewData["WelcomeMessage"] </div>
Comments